package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/18 14:15
 */
public class LeetCode17 {

    /**
     * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
     * <p>
     * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母
     *
     * @param digits 电话号
     * @return
     */
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.isEmpty()) return result;
        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        dfs(map, digits, result, 0, new StringBuilder());
        return result;
    }

    private void dfs(Map<Character, String> map, String digits, List<String> result, Integer startIndex, StringBuilder innerResult) {
        if (innerResult.length() == digits.length()) {
            result.add(innerResult.toString());
            return;
        }
        char digit = digits.charAt(startIndex);
        String letters = map.get(digit);
        int lettersCount = letters.length();
        for (int i = 0; i < lettersCount; i++) {
            innerResult.append(letters.charAt(i));
            dfs(map, digits, result, startIndex + 1, innerResult);
            innerResult.deleteCharAt(innerResult.length() - 1);
        }
    }

    public static void main(String[] args) {
        LeetCode17 leetCode17 = new LeetCode17();
        System.out.println(leetCode17.letterCombinations("23"));
    }
}